package com.ygj.purchaseOrderReceipt.action;

import static com.base.common.GlobaConstants.ACTION_RESULT_DATA;
import static com.base.common.GlobaConstants.ASYNCHRONOUS;
import static com.base.common.GlobaConstants.CONTENT_TYPE;
import static com.base.common.GlobaConstants.CONTENT_TYPE_JSON;
import static com.base.common.GlobaConstants.JSON;
import static com.base.common.GlobaConstants.ROOT;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.base.common.action.AbstractBaseAction;
import com.base.common.bean.ActionResult;
import com.cjy.common.bean.OpAttributes;
import com.ygj.purchaseOrderReceipt.service.PurchaseOrderReceiptService;

@Action("/purchaseOrderReceipt")
@Results({ @Result(name = ASYNCHRONOUS, type = JSON, params = { ROOT,
		ACTION_RESULT_DATA, CONTENT_TYPE, CONTENT_TYPE_JSON }) })
public class PurchaseOrderReceiptAction extends AbstractBaseAction{

	private static final long serialVersionUID = -874865066090195269L;

	private static Logger log = LoggerFactory
			.getLogger(PurchaseOrderReceiptAction.class);

	@Resource
	private PurchaseOrderReceiptService purchaseOrderReceiptService;
	
	private String skey;
	
	private String username;
	
	private int page;
	
	private String fmen;

	public String getSkey() {
		return skey;
	}

	public void setSkey(String skey) {
		this.skey = skey;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public String getFmen() {
		return fmen;
	}

	public void setFmen(String fmen) {
		this.fmen = fmen;
	}

	@Action("/queryPurchaseOrderReceiptCounts")
	public String queryPurchaseOrderReceiptCounts() {
		try {
			actionResult = new ActionResult(new OpAttributes()
					.add("code", 1)
					.add("result",
							new OpAttributes()
									.add("totalCounts",
											purchaseOrderReceiptService
													.queryPurchaseOrderReceiptTotalCounts(username))));
		} catch (Exception e) {
			log.error("purchaseOrderReceipt queryPurchaseOrderReceiptCounts error",
					e.fillInStackTrace());
			actionResult = new ActionResult(new OpAttributes().add("code", 0));
		}
		
		return returnType();
	}

	@Action("/queryPurchaseOrderReceipts")
	public String queryPurchaseOrderReceipts() {
		try {
			actionResult = new ActionResult(new OpAttributes().add("code", 1).add(
					"result",
					purchaseOrderReceiptService
							.queryPurchaseOrderReceipts(username, page)));
		} catch (Exception e) {
			log.error("purchaseOrderReceipt queryPurchaseOrderReceipts error",
					e.toString());
			e.printStackTrace();
			actionResult = new ActionResult(new OpAttributes().add("code", 0));
		}
		
		return returnType();
	}
	
	@Action("/queryPurchaseOrderReceiptDetail")
	public String queryPurchaseOrderReceiptDetail() {
		try {
			actionResult = new ActionResult(new OpAttributes().add("code", 1).add("result", purchaseOrderReceiptService.queryPurchaseOrderReceiptDetail(skey)));
		} catch (Exception e) {
			log.error("purchaseOrderReceipt queryPurchaseOrderReceiptDetail error",
					e.toString());
			e.printStackTrace();
			actionResult = new ActionResult(new OpAttributes().add("code", 0));
		}
		
		return returnType();
	}
	
	@Action("/sign")
	public String sign() {
		try {
			actionResult = new ActionResult(new OpAttributes().add("code", purchaseOrderReceiptService.sign(fmen, skey)));
		} catch (Exception e) {
			log.error("queryOfficialDocumentSignDetail exception", e);
			e.printStackTrace();
			actionResult = new ActionResult(new OpAttributes().add("code", 0));
		}
		
		return returnType();
	}
	
}
